package com.atguigu.gulimall.order.interceptor;

import com.atguigu.common.vo.MemberResponseVo;
import com.atguigu.gulimall.order.config.RedisEx;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 满超
 * @date 2020/06/26
 **/
@Component
public class OrderIntercept implements HandlerInterceptor {
    //登录信息共享
    public static ThreadLocal<MemberResponseVo> threadLocal = new ThreadLocal<>();

    //目标执行之前,登陆了之后才可以访问
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //自定义请求路径拦截
        String uri = request.getRequestURI();
        //请求路径拦截器
        boolean match = new AntPathMatcher().match("/order/order/status/**", uri);

        //放行支付
        boolean match1 = new AntPathMatcher().match("/payed/notify", uri);
        if (match || match1) {
            return true;
        }

        //从session取出登录信息
        MemberResponseVo attribute = (MemberResponseVo) request.getSession().getAttribute(RedisEx.LOGIN_USER);
        if (attribute != null) {
            //登录了
            threadLocal.set(attribute);
            return true;
        } else {
            request.getSession().setAttribute("msg","请登录后重试");
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}